package com.raising.utils;

import com.raising.framework.spring.SpringUtils;
import com.raising.modules.sys.entity.Elog;
import com.raising.modules.sys.entity.Olog;
import com.raising.modules.sys.entity.SLog;
import com.raising.modules.sys.entity.User;
import com.raising.modules.sys.service.ElogService;
import com.raising.modules.sys.service.SLogService;
import com.raising.modules.task.TaskLog;
import com.raising.modules.task.TaskOlog;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 日志工具
 * 
 * @author gaoy
 * 
 */
public class LogUtils {

	/**
	 * 保存日志
	 * @param request 请求
	 * @param user 用户
	 * @param timeConsuming 耗时
	 */
	public static void saveSLog(HttpServletRequest request, User user, String timeConsuming) {
		saveSLog(request, user, null,timeConsuming);
	}


	/**
	 * 保存日志
	 * @param request 请求
	 * @param user 用户
	 * @param ex 错误
	 * @param timeConsuming 耗时
	 */
	public static void saveSLog(HttpServletRequest request, User user,
			Exception ex,String timeConsuming) {
		SLog log = new SLog();
		log.setMenu(getMenuPath());
		log.setRemoteAddr(RequestUtils.getRemoteAddr(request));
		log.setUserAgent(request.getHeader("user-agent"));
		log.setRequestUri(request.getRequestURI());
		log.setParams(request.getParameterMap());
		log.setMethod(request.getMethod());
		if(user!=null){
			log.setCreateBy(user.getId());
		}
		log.setCreateDate(new Date());
		log.setTimeConsuming(timeConsuming);
		if (ex != null) {
			log.setException(ex.getMessage());
		}
		TaskLog.getSingleton().add(log);
	}

	/**
	 * 保存操作日志
	 * @author GaoYuan
	 * @datetime 2018/1/17 下午2:29
	 * @param optContent 操作内容
	 */
	public static void saveOlog(String optContent) {
		Olog log = new Olog();
		log.setContent(optContent);
		User user = UserUtils.getLoginUser();
		if(user!=null){
			log.setCreateBy(UserUtils.getUserId());
		}
		log.setCreateDate(new Date());
		TaskOlog.getSingleton().add(log);
	}
	public static void saveOlog(String optContent,String param) {
		Olog log = new Olog();
		log.setContent(optContent);
		log.setParam(param);
		User user = UserUtils.getLoginUser();
		if(user!=null){
			log.setCreateBy(UserUtils.getUserId());
		}
		log.setCreateDate(new Date());
		TaskOlog.getSingleton().add(log);
	}

	/**
	 * 获取菜单名称
	 */
	public static String getMenuPath(){

		return "";
	}
}
